* Extensions File: Earnings

set more off

clear

cap n log close

log using 2c2-Earnings.log, replace

use ./temp/1a-US-Master2-Wage.dta, clear
g valid=(sex==1 & approx_age>=16 & age>=22 & age<=70 & ethn!=1 & classwkr!=0)
g validus=(sex==1 & age>=22 & age<=70 & ethn==1 & classwkr!=0)
keep if (valid==1 | validus==1)

*** Prepare basic covariates
gen HS=(educ>2 & educ<=6)
gen SCOL=(educ>=7 & educ<=9)
gen COL=(educ>=10 & educ!=.)
gen fluent=(speakeng==3 | speakeng==4) //"only English" or "very well"
gen self=(classwkr==1)
gen inctot2=inctot
replace inctot2=1000 if inctot2<1000
for any inctot inctot2: replace X=ln(X)

*** Generate group shares
gen temp0=perwt
egen temp0a=sum(temp0), by(ethn ind1990)
egen temp0b=sum(temp0), by(ind1990)
egen temp0c=sum(temp0), by(ethn)
gen temp1=perwt if self==1
egen temp1a=sum(temp1), by(ethn ind1990)
egen temp1b=sum(temp1), by(ind1990)
egen temp1c=sum(temp1), by(ethn)
gen Gsetot=temp1c/temp0c
gen Gseind=temp1a/temp0c
gen Gtlind=(temp0a-temp1a)/temp0c
drop temp*
sum

*** Keep immigrant set
gen IMM=(ethn!=1)
su IMM
keep if IMM==1
drop IMM
sum

*** Demean
for var G*: egen temp1=mean(X) \ egen temp2=sd(X) \ replace X=(X-temp1)/temp2 \ drop temp*
for var Gsetot Gseind Gtlind: gen IX=X*self

*** Generate covariates
gen age_grp=0
for num 20(10)60: replace age_grp=X if age>X & age!=.
gen age_imm_grp=0
for num 20(10)60: replace age_imm_grp=X if approx_age>X & approx_age!=.
local covariates1 fluent HS COL SCOL _I*
xi i.ethn
renpfix _I I
xi i.age_grp i.age_imm_grp
egen clust=group(ethn ind1990)
egen msaind=group(metaread ind1990)
egen msaindeth=group(metaread ind1990 ethn)

**************************************************
*** Do self employed earn more when clustered?
**************************************************

* Self employed
eststo clear
estimates clear
eststo: qui areg inctot2 Gseind `covariates1' if self==1 [aw=perwt], a(msaind) cl(clust) 
eststo: qui areg inctot2 Gseind Gsetot `covariates1' if self==1 [aw=perwt], a(msaind) cl(clust) 
eststo: qui areg inctot2 Gseind Gsetot Gtlind `covariates1' if self==1 [aw=perwt], a(msaind) cl(clust) 
eststo: qui areg inctot2 Gseind `covariates1' Ieth* if self==1 [aw=perwt], a(msaind) cl(clust) 
eststo: qui areg inctot2 Gseind Gtlind `covariates1' Ieth* if self==1 [aw=perwt], a(msaind) cl(clust) 

* Wage comparison
eststo: qui areg inctot2 Gseind `covariates1' if self==0 [aw=perwt], a(msaind) cl(clust) 
eststo: qui areg inctot2 Gseind Gsetot `covariates1' if self==0 [aw=perwt], a(msaind) cl(clust) 
eststo: qui areg inctot2 Gseind Gsetot Gtlind `covariates1' if self==0 [aw=perwt], a(msaind) cl(clust) 
eststo: qui areg inctot2 Gseind `covariates1' Ieth* if self==0 [aw=perwt], a(msaind) cl(clust) 
eststo: qui areg inctot2 Gseind Gtlind `covariates1' Ieth* if self==0 [aw=perwt], a(msaind) cl(clust) 
esttab _all using ".\output\T7.csv", replace se r2 ar2 b(%9.3f) se(%9.3f) star( + .1 ++ .05 +++ .01) order(Gseind Gsetot Gtlind) keep(Gseind Gsetot Gtlind) 

**************************************************
*** Do self employed earn more than wage workers?
**************************************************

eststo clear
estimates clear
eststo: qui areg inctot2 self Gseind IGseind `covariates1' [aw=perwt], a(msaind) cl(clust) 
eststo: qui areg inctot2 self Gseind IGseind Gsetot IGsetot `covariates1' [aw=perwt], a(msaind) cl(clust) 
eststo: qui areg inctot2 self Gseind IGseind Gsetot IGsetot Gtlind IGtlind `covariates1' [aw=perwt], a(msaind) cl(clust) 
eststo: qui areg inctot2 self `covariates1' [aw=perwt], a(msaindeth) cl(clust) 
eststo: qui areg inctot2 self IGseind `covariates1' [aw=perwt], a(msaindeth) cl(clust) 
eststo: qui areg inctot2 self IGseind IGsetot IGtlind `covariates1' [aw=perwt], a(msaindeth) cl(clust) 
esttab _all using ".\output\TA9.csv", replace se r2 ar2 b(%9.3f) se(%9.3f) star( + .1 ++ .05 +++ .01) order(self Gseind IGseind Gsetot IGsetot Gtlind IGtlind) keep(self Gseind IGseind Gsetot IGsetot Gtlind IGtlind)

** End of program
log off